/**
 *  Copyright (C) 2010  Thomas Volk
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.bookmarks.core.service;

import java.net.URL;
import java.util.List;

import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.bookmarks.core.beans.FtpResource;
import com.googlecode.bookmarks.core.beans.RemoteResource;
import com.googlecode.bookmarks.core.beans.Resource;
import com.googlecode.bookmarks.core.beans.SecureWebpage;
import com.googlecode.bookmarks.core.beans.Webpage;
import com.googlecode.bookmarks.core.dispatcher.DispatcherFactory;
import com.googlecode.bookmarks.core.dispatcher.ResourceDispatcher;

@Transactional
@Service
public class ResourceServiceImpl extends AbstractPersistenceService implements
		ResourceService {
	@Autowired
	private DispatcherFactory dispatcherFactory;

	public DispatcherFactory getDispatcherFactory() {
		return dispatcherFactory;
	}

	public List<Webpage> getAllWebpages() {
		Query query = getEntityManager().createNamedQuery("getAllWebpages");
		return query.getResultList();
	}

	public void deleteResource(int resourceId) {
		Resource resource = getEntityManager().find(Resource.class, resourceId);
		deleteResource(resource);
	}

	public void deleteResource(Resource resource) {
		getEntityManager().remove(resource);
	}

	public RemoteResource createResource(URL url, String title) {
		ResourceDispatcher.Handler handler = new ResourceDispatcher.Handler() {
			@Override
			public Object handleWebpage(Webpage webpage) {
				return webpage;
			}

			@Override
			public Object handleSecureWebpage(SecureWebpage sWebpage) {
				return sWebpage;
			}

			@Override
			public Object handleFtpResource(FtpResource ftpResource) {
				return ftpResource;
			}

		};
		RemoteResource resource = (RemoteResource) getDispatcherFactory()
				.createUrlDispatcher(url).dispatch(handler);
		resource.setTitle(title);
		updateResource(resource);
		getEntityManager().refresh(resource);
		return resource;
	}

	@Override
	public void updateResource(int id, URL url, String title) {
		RemoteResource resource = (RemoteResource) getResource(id);
		resource.setTitle(title);
		resource.setUrl(url);
		updateResource(resource);
	}

	public void updateResource(RemoteResource resource) {
		getEntityManager().persist(resource);
	}

	@Override
	public Resource getResource(int id) {
		return getEntityManager().find(Resource.class, id);
	}
}
